package com.aeks.servlet;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.aeks.user.AboutUser;

public class AddStopServlet extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		
		String fileName = "stopwords.txt";
		String path = this.getServletContext().getRealPath("/") + "material\\" + fileName;
		//System.out.println(path);
		
		boolean isSuccess = true;
		String action = request.getParameter("action");
		if (action != null && action.equals("add")) {
			String stopWord = request.getParameter("addStop");
			
			HttpSession session = request.getSession();
			String username = (String)session.getAttribute("username");
			
			if(stopWord != null && !stopWord.equals("")) {
				File f = new File(path);
				FileWriter fw = new FileWriter(f, true);
				BufferedWriter bw = new BufferedWriter(fw);
				bw.newLine();
				bw.write(stopWord);
				bw.close();
				fw.close();
			} else {
				isSuccess = false;
			}
		} else {
			isSuccess = false;
		}
		
		if(isSuccess) {
			out.println("<script language='javascript'>");
			out.println("alert('添加成功！');");
			out.println("window.location.href='main.jsp'");
			out.println("</script>");
		} else {
			out.println("<script language='javascript'>");
			out.println("alert('添加失败，请重新添加!');");
			out.println("window.location.href='main.jsp'");
			out.println("</script>");
		}
		
		out.flush();
		out.close();
	}

}
